<template>
	<view >
		<vol-form @extraClick="extraClick" :load-key="true" ref="form" :form-options.sync="editFormOptions"
			:formFields.sync="editFormFields">
		</vol-form>

		<view class="btns">
			<view class="btn">
				<u-button type="primary" @click="reset" shape="circle" text="重置表单"></u-button>
			</view>
			<view class="btn">
				<u-button type="success" @click="save" shape="circle" text="保存"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import options from './canteen_yd_dxsOptions.js';
	let _options = options();
	//_options.extend = extend;
	export default {
		data() {
			return {
				ip:'',
				opt:_options,
				editFormFields:_options.editFormFields,
				editFormOptions:_options.editFormOptions,
				count:5,
				value:3,
				depart:[[]],
				show: false,
			}
		},
		onShow() {
					this.$nextTick(()=>{
						this.editFormFields.yd_date=this.getWeekDay()[0].saturDay;
						//console.log(this.getWeekDay()[0].saturDay);//获取本周6的日期
						//this.$refs.form.reset();
						//this.$toast("表单已重置")
						/* for(var item in this.editFormFields){
							 if (item.substring(0,1)=='q'){
								this.editFormFields[item]='A';
							}
							//if(item=="q1")this.editFormFields[item]='A';
							//if(item=="q2")this.editFormFields[item]='A';
						} */
					})
				
				},
				onBackPress(options) {//返回return 阻止按钮起效
					//console.log("kkkk");
				   /* if (options.from === 'navigateBack') {
				        return false
				    }
				    uni.navigateBack({
				        delta: 100
				    })*/
				    return true;//返回return 阻止按钮起效
				},
		methods: {
			viewClick() {
				//获取生成页面grid的对象
				console.log(this.$refs.grid.searchFormFields)
			},
			confirm(e) {
			                //console.log(e.value[0])
							this.editFormFields.department=e.value[0]
			                this.show = false
						},
			cancelHand(e){
				this.show = false
			},
			save(){
				if (this.$refs.form.validate()) {
					let savedata={//判断前端对象属性为数组 
						mainData:this.editFormFields,
						delKeys: null,
						detailData: null
					}
					var keys=Object.keys(savedata.mainData);//需要把selectList字段类型转换为逗号分割的字符串
					      for (let index = 0; index < keys.length; index++) {
					        const e = keys[index];
							console.log(savedata.mainData[e]);
					        if(Array.isArray(savedata.mainData[e])){//判断savedata.mainData对象中哪些属性为数组的，如果是转为字符串
									console.log(savedata.mainData[e]+"array..........");
					            savedata.mainData[e]=savedata.mainData[e].join(',');//,转换为，分割的字符串，用于volform提交数据
					        }
					      }
				
					this.http.post("/api/canteen_yd_dxs/Add", savedata, true).then((s) => {
						if (s.status){
						  this.$toast("保存成功");
						  uni.navigateTo({
						  	url: "/pages/mytable/canteen_yd/end_back"
						  })
						}else{
						  this.$toast(s.message);
						}
						
						
						})
				}
				/* this.$refs.form.validate()
				let savedata={//判断前端对象属性为数组 
								mainData:this.editFormFields,
								delKeys: null,
								detailData: null
									}
				this.savecheck();
				if (this.editFormFields.department==""){
					this.$toast("请选择部门");
					return;
				}
				this.http.post("/api/canteen_yd/Add", savedata, true).then((s) => {
						this.$toast("保存成功");
						uni.navigateTo({
							url: "/pages/mytable/canteen_yd/end_back"
						})
						}) */
			},
			savecheck(){
				//console.log(this.editFormFields);
				
			},
			getWeekDay() {
			 let _date = new Date();
			 let _nowTime = _date.getTime();
			 let _week = _date.getDay();
			 let _dayLongTime = 24 * 60 * 60 * 1000;
			 let _furtureSundayTimes = _nowTime + (7 - _week) * _dayLongTime;
			 let _furtureSaturdayTimes = _nowTime + (6 - _week) * _dayLongTime;
			 _furtureSundayTimes = new Date(_furtureSundayTimes);
			 _furtureSaturdayTimes = new Date(_furtureSaturdayTimes);
			 // staurday
			 let _satYear = _furtureSaturdayTimes.getFullYear();
			 let _satMonth = _furtureSaturdayTimes.getMonth() + 1;
			 let _satDay = _furtureSaturdayTimes.getDate();
			 //sunday
			 let _sunYear = _furtureSundayTimes.getFullYear();
			 let _sunMonth = _furtureSundayTimes.getMonth() + 1;
			 let _sunDay = _furtureSundayTimes.getDate();
			 _satMonth = _satMonth >= 10 ? _satMonth : '0' + _satMonth;
			 _satDay = _satDay >= 10 ? _satDay : '0' + _satDay;
			 _sunMonth = _sunMonth >= 10 ? _sunMonth : '0' + _sunMonth;
			 _sunDay = _sunDay >= 10 ? _sunDay : '0' + _sunDay;
			 let _mealSunDay = _satYear+'-'+_satMonth+'-'+_satDay;
			 let _mealSaturDay = _sunYear+ '-'+_sunMonth+'-'+_sunDay;
			 let _weekendDay = [{
			  saturDay: _mealSunDay
			 }, {
			  sunDay: _mealSaturDay
			 }]
			 return _weekendDay;
			}
			/* getUserIP (onNewIP) { // 获取ip地址
			    let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
			    let pc = new MyPeerConnection({iceServers: []})
			    let noop = function () {}
			    let localIPs = {}
			    let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g
			    function iterateIP (ip) {
			        if (!localIPs[ip]) onNewIP(ip)
			        localIPs[ip] = true
			    }
			    pc.createDataChannel('')
			    pc.createOffer().then(function (sdp) {
			        sdp.sdp.split('\n').forEach(function (line) {
			        if (line.indexOf('candidate') < 0) return
			        line.match(ipRegex).forEach(iterateIP)
			        })
			        pc.setLocalDescription(sdp, noop, noop)
			    }).catch(function (reason) {
			    // An error occurred, so handle the failure to connect
			    })
			    // seen for candidate events
			    pc.onicecandidate = function (ice) {
			        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return
			        ice.candidate.candidate.match(ipRegex).forEach(iterateIP)
			    }
			}, */
		}
	}
</script>

<style lang="less" scoped>
	.form-test {
		margin-top: -20rpx;
		background: #fbfbfb;
		padding-top: 20rpx;
	}

	.btns {
		display: flex;
		padding: 0rpx 20rpx;

		.btn {
			flex: 1;
			padding: 20rpx;
		}
	}
</style>